package com.e.library.dialog;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by lxy on 2017/1/4.
 */

public abstract class EDialogFragment extends DialogFragment {

    private Unbinder unbinder;
    protected abstract int layout();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(layout(),container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    public void show(FragmentManager manager) {
        show(manager,this.getClass().getName());
    }

    private ELoading mLoading;
    protected void loading(){
        if (mLoading == null){
            mLoading = new ELoading(getContext());
        }
        if (!mLoading.isShowing()){
            mLoading.showLoading("");
        }
    }

    protected void hideLoading(){
        if (mLoading !=null && mLoading.isShowing()){
            mLoading.dismiss();
        }
    }

    protected void hideLoading(String msg){
        hideLoading();
        toast(msg);
    }

    protected void toast(String toast){
        Toast.makeText(getContext(),toast,Toast.LENGTH_SHORT).show();
    }

    protected void toast(int toast){
        Toast.makeText(getContext(),toast,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

}
